package com.jbit.examSystem.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jbit.examSystem.exception.AddException;
import com.jbit.examSystem.exception.RemoveException;
import com.jbit.examSystem.param.PageParam;
import com.jbit.examSystem.pojo.*;
import com.jbit.examSystem.param.TestPaperParam;
import com.jbit.examSystem.param.TestPaperParam;
import com.jbit.examSystem.service.CourseService;
import com.jbit.examSystem.service.GradeService;
import com.jbit.examSystem.service.TestPaperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.*;

@Controller
@RequestMapping("testPaper")
public class TestPaperController {
    @Autowired
    private TestPaperService testPaperService;
    @Autowired
    private GradeService gradeService;
    @Autowired
    private CourseService courseService;

    @ModelAttribute("grades")
    public List<Grade> getAllGrades(HttpSession session){
        List<Grade> grades = gradeService.getAllGrade();
        session.setAttribute("grades",grades);

        return grades;
    }

    @ModelAttribute("courses")
    public List<Course> getAllCourses(HttpSession session){
        List<Course> courses = courseService.getAllCourse();
        session.setAttribute("courses",courses);

        return courses;
    }

    @RequestMapping("list")
    @ResponseBody
    public TableDataInfo list(TestPaperParam param, PageParam page){
        TableDataInfo respData = new TableDataInfo();
        try {
            PageHelper.startPage(page.getPageNum(),page.getPageSize(),true,true,false);
            List<TestPaper> testPapers = testPaperService.list(param);

            respData.succeed(testPapers, 0);
        } catch (Exception e){
            respData.setCode(500);
            e.printStackTrace();
        }
        return respData;
    }

    @RequestMapping("findById/{id}")
    public String findById(@PathVariable int id, HttpSession session){
        List<Question> questionList = testPaperService.getQuestionByTestPaperId(id);
        TestPaper testPaper = testPaperService.findById(id);
        session.setAttribute("testPaper",testPaper);
        session.setAttribute("questionList",questionList);

        return "onlineExam/testPaper/update";
    }

    @RequestMapping("addTestPaper")
    @ResponseBody
    public Map<String,Object> add(TestPaper testPaper,Integer teacherId,Integer[] questionIds){
        Map<String,Object> map = new HashMap<>();
        testPaper.setTeacherId(teacherId);
        testPaper.setCreateTime(new Date());
        try {
            testPaperService.add(testPaper);
            testPaperService.addDetail(questionIds,testPaper.getId());
            map.put("msg", "添加成功");
            map.put("code", 0);

        } catch (AddException e) {
            map.put("msg", "添加失败,失败原因：" + e.getMessage());
            map.put("code", 500);
        }

        return map;
    }

    @RequestMapping("update")
    @ResponseBody
    public Map<String,Object> update(TestPaper testPaper){
        Map<String,Object> map = new HashMap<>();

        try {
            testPaperService.modify(testPaper);
            map.put("msg", "修改成功");
            map.put("code", 0);

        } catch (Exception e) {
            map.put("msg", "修改失败,失败原因：" + e.getMessage());
            map.put("code", 500);
        }
        return map;
    }

    @RequestMapping("remove")
    @ResponseBody
    public Map<String,Object> remove(Integer[] ids){
        Map<String,Object> map = new HashMap<>();
        try {
            testPaperService.remove(ids);
            map.put("msg", "删除成功");
            map.put("code", 0);

        } catch (RemoveException e) {
            map.put("msg", "删除失败,失败原因：" + e.getMessage());
            map.put("code", 500);
        }catch (Exception e) {
            map.put("msg", "删除失败,失败原因：该试卷已被引用，禁止删除！");
            map.put("code", 500);
        }
        return map;
    }

}
